<?php

namespace App\Admin\Controllers;

use App\Models\TaskStep;
use App\Models\Task;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TaskStepController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('任务步骤')
            ->description('任务步骤')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new TaskStep);

        $grid->id('Id');
        $grid->task_id('任务名称')->display(function($task_id){
            $data = Task::where('id',$task_id)->first();
            if ($data) {
                return $data->name;
            }else{
                return '无';
            }
        });
        $grid->step('第几步');
        $grid->step_detail('步骤描述')->display(function ($content) {
            $content = htmlspecialchars_decode($content);
            return $content;
        });
        $grid->step_img('步骤截图')->image('/uploads/', 200, 200);
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->disableRowSelector();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('task_id','选择任务')->select('/admin/task/get_task_list');
            $filter->where(function ($query) {
                $query->whereHas('task', function ($query) {
                    $query->where('name', 'like', "%{$this->input}%");
                });
            }, '任务名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(TaskStep::findOrFail($id));

        $show->id('Id');
        $show->task_id('Task id');
        $show->step('Step');
        $show->step_detail('Step detail');
        $show->step_img('Step img');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new TaskStep);

        $form->select('task_id', '任务名称')->options('/admin/task/get_task_list');
        $form->number('step', '第几步');
        $form->editor('step_detail', '步骤描述');
        $form->multipleImage('step_img', '步骤截图')->rules('image',[
            'image'   =>'请上传图片文件',
        ])->uniqueName()->removable();

        return $form;
    }
}
